package com.easytouch.vmhdl.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.easytouch.core.utils.Log;
import com.easytouch.vmhdl.R;
import com.easytouch.vmhdl.activity.ManagerActivity;
import com.easytouch.vmhdl.interfaces.IManagerTestView;
import com.easytouch.vmhdl.presenter.ManagerTestPresenter;

/**
 * Created by gulin on 2018/1/22.
 */

public class TestFragment extends BaseMgrFragment implements View.OnClickListener, IManagerTestView {

    private Log mLogger = new Log("TestFragment.txt");

    private ManagerTestPresenter mPresenter;

    private TextView mTvResult1;
    private TextView mTvResult2;
    private TextView mTvResult3;
    private TextView mTvResult4;
    private ProgressBar mPbUrl1;
    private ProgressBar mPbUrl2;
    private ProgressBar mPbUrl3;
    private ProgressBar mPbUrl4;
    private Button mBtStart;
    private Button mBtBack;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        setTitle(R.string.mgr_test_title);

        mTvResult1 = view.findViewById(R.id.tv_result1);
        mTvResult2 = view.findViewById(R.id.tv_result2);
        mTvResult3 = view.findViewById(R.id.tv_result3);
        mTvResult4 = view.findViewById(R.id.tv_result4);
        mPbUrl1 = view.findViewById(R.id.pb_url1);
        mPbUrl2 = view.findViewById(R.id.pb_url2);
        mPbUrl3 = view.findViewById(R.id.pb_url3);
        mPbUrl4 = view.findViewById(R.id.pb_url4);
        mBtStart = view.findViewById(R.id.bt_start);
        mBtBack = view.findViewById(R.id.bt_back);

        mBtStart.setOnClickListener(this);
        mBtBack.setOnClickListener(this);

        mPresenter = new ManagerTestPresenter(this);

        initData();

        return view;
    }

    private void initData()
    {
        mTvResult1.setText("");
        mTvResult2.setText("");
        mTvResult3.setText("");
        mTvResult4.setText("");
        mPbUrl1.setProgress(0);
        mPbUrl2.setProgress(0);
        mPbUrl3.setProgress(0);
        mPbUrl4.setProgress(0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_start:
                mBtStart.setEnabled(false);
                mBtStart.setText(getString(R.string.mgr_test_ing));
                initData();
                mPresenter.doTest();
                break;
            case R.id.bt_back:
                goToFragment(ManagerActivity.FRAGMENT_MENU, null);
                break;
        }
    }

    @Override
    public void updateProgress(int pProgressBar, int pPercent)
    {
        ProgressBar pb = null;
        switch (pProgressBar)
        {
            case 1:
                pb = mPbUrl1;
                break;
            case 2:
                pb = mPbUrl2;
                break;
            case 3:
                pb = mPbUrl3;
                break;
            case 4:
                pb = mPbUrl4;
                break;
        }
        if(pb != null)
        {
            pb.setProgress(pPercent);
        }
    }

    @Override
    public void updateResult(int pResult, int pTotal, double pPercent, int pTimeout, int pIOError, int pDNSError, long pMinTime, long pMaxTime, long pMeanTime)
    {
        TextView tv = null;
        switch (pResult)
        {
            case 1:
                tv = mTvResult1;
                break;
            case 2:
                tv = mTvResult2;
                break;
            case 3:
                tv = mTvResult3;
                break;
            case 4:
                tv = mTvResult4;
                break;
        }
        if(tv != null)
        {
            String result = getString(R.string.mgr_test_result);
            result = String.format(result, String.valueOf(pTotal), String.valueOf(pPercent), String.valueOf(pTimeout), String.valueOf(pIOError), String.valueOf(pDNSError), String.valueOf(pMinTime), String.valueOf(pMaxTime), String.valueOf(pMeanTime));
            tv.setText(result);
        }
    }

    @Override
    public void finishTest()
    {
        mBtStart.setEnabled(true);
        mBtStart.setText(getString(R.string.mgr_test_start));
    }

    @Override
    public void onDestroyView() {
        mPresenter.stopTest();
        super.onDestroyView();
    }
}
